package com.jdiv.extensions.samples;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;

import com.jdiv.extensions.JFpg;
import com.jdiv.extensions.JFpgImage;


public class FpgViewer extends JFrame{

	private static final long serialVersionUID = 1L;
	private DecimalFormat df = new DecimalFormat("000");
	
	public FpgViewer(){}
	 
	 public FpgViewer(String file){
		 JFpg jFpg =new JFpg(file);
		 creaVentana(jFpg);	 
	 }
	 
	 public void creaVentana(JFpg jFpg){
		 	
		   setTitle("FpgViewer");
	       setLayout(null);
	       setBounds(0,0,225,332);
	       setResizable(false); 
	       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	       
	       ArrayList<JFpgImage> img=jFpg.getFpg();
	   
	       JPanel info=new JPanel();
	       info.add(new JLabel("Fpg Type: "+jFpg.getFpgType()));
	       info.add(new JLabel("Fpg Images: "+jFpg.getSize()));
	       
	       JPanel panel1=new JPanel();
		   panel1.setLayout(null);
	   
	   	   int pAlto=0;
		 
	       for (int i=0;i<img.size();i++){
	      
	       JPanel panel =new JPanel();
	       panel.setLayout(null);
	       
	       pAlto=i*100;
	       
	       panel.setBounds(0,pAlto,200,100);
	       panel.setOpaque(true);
	       panel.setBackground(Color.black);
	      
	       final JFpgImage fImg=img.get(i);
	             
	       JLabel back=new JLabel(new ImageIcon("res/background.jpg"));
	       JLabel lbl1=new JLabel(new ImageIcon(fImg.getImagen().getScaledInstance(50,50,0)));
	       JLabel lbl2=new JLabel("Codigo: "+df.format(fImg.getCodigo()));
	       JLabel lbl3=new JLabel("Alto: "+fImg.getAlto());
	       JLabel lbl4=new JLabel("Ancho: "+fImg.getAncho());
	       JLabel lbl5=new JLabel("Nombre: "+fImg.getNombre());
	       JLabel lbl6=new JLabel("Descripcion: "+fImg.getDescripcion());
	       
	       lbl1.setForeground(Color.white);
	       lbl2.setForeground(Color.white);
	       lbl3.setForeground(Color.white);
	       lbl4.setForeground(Color.white);
	       lbl5.setForeground(Color.white);
	       lbl6.setForeground(Color.white);
	       
	       back.setBounds(0,0,200,100);
	       lbl1.setBounds(140,10,50,50);
	       lbl2.setBounds(5,5,70,20);
	       lbl3.setBounds(5,20,70,20);
	       lbl4.setBounds(5,40,70,20);
	       lbl5.setBounds(5,60,150,20);
	       lbl6.setBounds(5,80,190,20);
	              
	        panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
	        
	        
	        panel.add(lbl1);
	        panel.add(lbl2);
	        panel.add(lbl3);
	        panel.add(lbl4);
	        panel.add(lbl5);
	        panel.add(lbl6);
	        panel.add(back);
	        
	      	panel.addMouseListener(new MouseAdapter(){
	    
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2) 
					muestraFpg(fImg);
				}	
			});
				
	       
	        panel1.add(panel);
	        }      
	    panel1.setPreferredSize(new Dimension(200,pAlto+100) );           
	    
	    JScrollPane scroll=new JScrollPane(panel1);
	    scroll.setBounds(0,0,218,300);
	    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	   
	      getContentPane().setLayout(new BorderLayout());
	      getContentPane().add(info,BorderLayout.NORTH);
	      getContentPane().add(scroll,BorderLayout.CENTER);
	      setVisible(true);
	      
	      if (jFpg.getFpgType().equals("fpg")) muestraPaleta(jFpg.getPaleta());
	 }
	 
	 public void muestraPaleta(BufferedImage paleta){
		 JFrame ventana = new JFrame();
		 ventana.setLayout(new FlowLayout());
		 ventana.setTitle("Paleta");
		 ventana.setSize(328,360);
		 ventana.setResizable(false); 
		 ventana.getContentPane().setBackground(Color.black);
		 ventana.getContentPane().add(new JLabel(new ImageIcon(paleta)));
		 ventana.setVisible(true);
	   
	 }
	 
	 public void muestraFpg(JFpgImage fImg){
		 
		     
		 JFrame ventana = new JFrame();
		 ventana.setLayout(new BorderLayout());
		 ventana.setTitle(fImg.getNombre());
		 ventana.setSize(fImg.getAncho(),fImg.getAlto()+135);
		 ventana.setResizable(false); 
		 
		 JPanel p1=new JPanel();
		 JPanel p2=new JPanel();
		 
		 p1.setLayout(new GridLayout(6,1));
		 p1.setOpaque(true);
		 p1.setBackground(Color.black);
		 
		 JLabel l0=new JLabel(new ImageIcon(fImg.getImagen()));
	     JLabel l1=new JLabel("Codigo: "+df.format(fImg.getCodigo()));
	     JLabel l2=new JLabel("Alto: "+fImg.getAlto());
	     JLabel l3=new JLabel("Ancho: "+fImg.getAncho());
	     JLabel l4=new JLabel("Nombre: "+fImg.getNombre());
	     JLabel l5=new JLabel("Descripcion: "+fImg.getDescripcion());
		 JLabel l6=new JLabel("Puntos de Control: "+fImg.getFlags());
	   
		 l1.setForeground(Color.white);
		 l2.setForeground(Color.white);
		 l3.setForeground(Color.white);
		 l4.setForeground(Color.white);
		 l5.setForeground(Color.white);
		 l6.setForeground(Color.white);
		       
		 p2.add(l0);
		 p1.add(l1);
		 p1.add(l2);
		 p1.add(l3);
		 p1.add(l4);
		 p1.add(l5);
		 p1.add(l6);
		  
		 ventana.getContentPane().add(p1,BorderLayout.NORTH);
		 ventana.getContentPane().add(p2,BorderLayout.CENTER);
		 ventana.setVisible(true);
		}
	 
	 public static void main(String[] args) {
		
		 FpgViewer fpgv=new FpgViewer("res/fpg/iceman.fpg");
		}

}
